package snooker.scoreboard;

import java.util.LinkedList;
import java.util.List;

import android.os.SystemClock;
import android.util.Log;

public class FrameHistory {
	private static final String LOG_TAG = "FrameHistory";
	private List<FrameState> history;
	private long startTime;
	
	public FrameHistory() {
		history = new LinkedList<FrameState>();
		startTime = SystemClock.elapsedRealtime();
	}
	
	public FrameHistory(List<FrameState> history) {
		this.history = history;
		startTime = SystemClock.elapsedRealtime();;
	}
	
	public void addState(int playerId, int frameId, Type type, int breakId,
			Ball ball, int score, int player1Score, int player2Score, int reds, boolean isLuckyShot) {
		long offset = SystemClock.elapsedRealtime() - startTime;
		FrameState newState = new FrameState(playerId, frameId, type, 
				Type.Foul.equals(type) ? 0 : breakId, 
				ball, score, player1Score, player2Score, reds, offset, isLuckyShot);
		Log.i(LOG_TAG, "add to history: " + newState.toString());
		history.add(newState);
	}
	
	public long getStartTime() {
		return startTime;
	}
	
	public List<FrameState> getHistory() {
		return history;
	}
	
	public FrameState getLastState() {
		if (history.size() > 0) {
			return history.get(history.size() - 1);
		} else {
			return null;
		}
	}
	
	public void undo() {
		Log.i(LOG_TAG, "<<<<<<<<<<<<<<<< UNDO >>>>>>>>>>>>>>>>");
		if (history.size() > 0) {
			history.remove(history.size() - 1);
		} 
	}
	
	public void debug() {
		for (FrameState fs: history) {
			Log.i(LOG_TAG, "HState=" + fs.toString());
		}
	}
}
